package org.marlin.http.proxy;

import org.marlin.http.annotation.HttpClient;
import org.marlin.http.handler.HttpHandler;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * init version 1.5.0
 * @author Jack ZG
 * @since 1.0.0
 */
public class HttpServiceRegister {
    private final Map<Class<?>, HttpProxyFactory<?>> knownX5s = new ConcurrentHashMap<>();


    public <T> T getHttpInfo(Class<T> type, Map<Class, HttpHandler> httpHandler) {
        if (!type.isInterface() || type.getAnnotation(HttpClient.class) == null) {
            throw new IllegalStateException("Type " + type + " is not annotation with @HttpClient.");
        }
        HttpProxyFactory<T> httpProxyFactory = (HttpProxyFactory<T>) this.knownX5s.get(type);
        if (httpProxyFactory == null) {
            httpProxyFactory = new HttpProxyFactory<>(type);
            this.knownX5s.put(type, httpProxyFactory);
        }
        return httpProxyFactory.newInstance(httpHandler);
    }
}

